﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using System.Collections.ObjectModel;
using bdnt_pets_system.Tools;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.Failures
{
    public partial class NewVehicleWindow : BaseWindow
    {
        DataServiceReference.DataServiceClient dataClient;
        public NewVehicleWindow(Tbl_Vehicle cuTbl_Vehicle)
        {
            InitializeComponent();
            if (cuTbl_Vehicle.Id == 0)
                this.Title = MyStrings.xjcl;
            else this.Title = MyStrings.xgcl;

            dataClient = new DataServiceReference.DataServiceClient();
            CuTbl_Vehicle = cuTbl_Vehicle;
            BingData();
            InitEvnet();
        }
        public Tbl_Vehicle CuTbl_Vehicle
        {
            get { return this.DataContext as Tbl_Vehicle; }
            set { this.DataContext = value; }
        }

        void BingData()
        {
            tbProducitonNo.SetBinding(TextBox.TextProperty, new Binding("ProducitonNo")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });

            tbVINNo.SetBinding(TextBox.TextProperty, new Binding("VINNo")
            {
                Mode = BindingMode.TwoWay
            });
            tbVPhase.SetBinding(TextBox.TextProperty, new Binding("VPhase") { Mode = BindingMode.TwoWay });
            tbVTYPE.SetBinding(TextBox.TextProperty, new Binding("VType") { Mode = BindingMode.TwoWay });
            tbVColor.SetBinding(TextBox.TextProperty, new Binding("VColor") { Mode = BindingMode.TwoWay });
            tbVNsColor.SetBinding(TextBox.TextProperty, new Binding("InteriorColor") { Mode = BindingMode.TwoWay });
            tbRemark.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });
            LoadComboxData();
        }

        void InitEvnet()
        {
            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);

            dataClient.SaveVehicle1Completed += new EventHandler<DataServiceReference.SaveVehicle1CompletedEventArgs>(dataClient_SaveVehicle1Completed);
        }

        void dataClient_SaveVehicle1Completed(object sender, DataServiceReference.SaveVehicle1CompletedEventArgs e)
        {
            if (e.Result > 0)
            {
                this.DialogResult = true;
            }
            else if (e.Result == -1)
            {
                errorMsg.Text = "已存在此车辆VINNo或流水号，不能重复添加，请检查！";
            }
            else
            {
                errorMsg.Text = MyStrings.bcsb;
            }
        }
        void LoadComboxData()
        {
            SystemServiceReference.SystemServiceClient sysClient = new SystemServiceReference.SystemServiceClient();
            sysClient.GetDictListByPidCompleted += new EventHandler<SystemServiceReference.GetDictListByPidCompletedEventArgs>(sysClient_GetDictListByPidCompleted);
            sysClient.GetDictListByPidAsync(28);
        }

        void sysClient_GetDictListByPidCompleted(object sender, SystemServiceReference.GetDictListByPidCompletedEventArgs e)
        {
            var res = e.Result.OrderBy(p => p.Sequence).ToList();
            cbVConfiguration.ItemsSource = res;
            cbVConfiguration.DisplayMemberPath = SessionManager.IsCN_ZH ? "DictName" : "en_DictName";
            cbVConfiguration.SelectedValuePath = "DictValue";
            if (!string.IsNullOrEmpty(CuTbl_Vehicle.VConfiguration))
            {
                var selectobj = e.Result.FirstOrDefault(p => p.DictValue == CuTbl_Vehicle.VConfiguration);
                cbVConfiguration.SelectedIndex = selectobj == null ? 0 : res.IndexOf(selectobj);
            }
            else cbVConfiguration.SelectedIndex = 0;
        }



        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 字段验证方法
        /// </summary>
        void BindgExpression()
        {
            tbProducitonNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindgExpression();
                if (vs.Errors.Count > 0)
                {
                    errorMsg.Text = MyStrings.sjtxbq;
                    return;
                }
                CuTbl_Vehicle.VConfiguration = cbVConfiguration.SelectedValue.ToString();
                var cuo = cbVConfiguration.SelectedItem as Tbl_DictInfo;
                CuTbl_Vehicle.VConfigurationName = cuo.DictName;
                CuTbl_Vehicle.en_VConfigurationName = cuo.en_DictName;
                dataClient.SaveVehicle1Async(CuTbl_Vehicle);
            }
            catch (Exception)
            {

            }
        }
    }
}
